/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import Connection.CheckInfo;
import Bussiness.AdminDAO;
import Bussiness.EmployeeDAO;
import Connection.Admin;
import Connection.DBUtil;
import Connection.Employee;
import javax.swing.JOptionPane;
/**
 *
 * @author SONY
 */
public class ChangePass extends javax.swing.JFrame {
    private String email;
    private String oldPassword;
    private String newPassword;
    private String confirmPassword;
    private String password;
    
    private Admin objAdmin = new Admin();
    private Employee objEmployee = new Employee();
    /**
     * Creates new form ChangePass
     */
    public ChangePass() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        rdAdmin = new javax.swing.JRadioButton();
        rdEmployee = new javax.swing.JRadioButton();
        txtEmail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jpwPassword = new javax.swing.JPasswordField();
        jpwNew = new javax.swing.JPasswordField();
        jpwConfirm = new javax.swing.JPasswordField();
        btnClear = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Change PassWord", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Email");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Password");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("New Password");

        buttonGroup1.add(rdAdmin);
        rdAdmin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        rdAdmin.setText("Administrator");
        rdAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdAdminActionPerformed(evt);
            }
        });

        buttonGroup1.add(rdEmployee);
        rdEmployee.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        rdEmployee.setText("Employee");
        rdEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdEmployeeActionPerformed(evt);
            }
        });

        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Comfirm Password");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rdAdmin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                        .addComponent(rdEmployee))
                    .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpwPassword, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpwNew, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpwConfirm, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(44, 44, 44))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpwPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpwNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jpwConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdEmployee)
                    .addComponent(rdAdmin))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        btnClear.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnClear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/delete.png"))); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/block.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnOk.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/accept.png"))); // NOI18N
        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnClear)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btnClear)
                    .addComponent(btnCancel))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rdAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdAdminActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdAdminActionPerformed

    private void rdEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdEmployeeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdEmployeeActionPerformed

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEmailActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        txtEmail.setText(null);
        jpwPassword.setText(null);
        jpwNew.setText(null);
        jpwConfirm.setText(null);
        txtEmail.requestFocus();
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        email = txtEmail.getText();
        oldPassword = String.valueOf(jpwPassword.getPassword());
        newPassword = String.valueOf(jpwNew.getPassword());
        confirmPassword = String.valueOf(jpwConfirm.getPassword());
        DBUtil db = new DBUtil();
        if (rdAdmin.isSelected()){
            if (CheckInfo.checkEmpty(email) && CheckInfo.checkEmpty(oldPassword) && CheckInfo.checkEmpty(newPassword) && CheckInfo.checkEmpty(confirmPassword)){
                if ( db.connect() != null) {
                objAdmin = AdminDAO.getAllAdmin(email);
                    if (objAdmin != null) {
                        if (objAdmin.getEmail().equals(email) && objAdmin.getPassword().equals(oldPassword)) {
                            if(!oldPassword.equals(newPassword)){
                                if(newPassword.equals(confirmPassword)){
                                    AdminDAO.ChangePassword(oldPassword, newPassword);
                                    JOptionPane.showMessageDialog(null, "You are change Password Succesfull");
                                    this.dispose();
                                } else{
                                    JOptionPane.showMessageDialog(null, "Please Enter Exccalty password");
                                    jpwNew.setText("");
                                    jpwConfirm.setText("");
                                    jpwNew.requestFocus();
                                }
                            } else{
                                JOptionPane.showMessageDialog(null, "Don't Enter Password like Old Password");
                                jpwNew.setText("");
                                jpwConfirm.setText("");
                                jpwNew.requestFocus();
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "Password error!");
                            jpwPassword.setText("");
                            jpwNew.setText("");
                            jpwConfirm.setText("");
                            jpwPassword.requestFocus();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Email Error!");
                        txtEmail.setText("");
                        jpwPassword.setText("");
                        jpwNew.setText("");
                        jpwConfirm.setText("");
                        txtEmail.requestFocus();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Don't conect to Serer");
                }
            } else{
                JOptionPane.showMessageDialog(null, "Please Enter Information Full");
            }
        }
        
        if (rdEmployee.isSelected()){
            if (CheckInfo.checkEmpty(email) && CheckInfo.checkEmpty(oldPassword) && CheckInfo.checkEmpty(newPassword) && CheckInfo.checkEmpty(confirmPassword)){
                if ( db.connect() != null) {
                objEmployee = EmployeeDAO.getLoginEmployee(email);
                    if (objEmployee != null) {
                        if (objEmployee.getEmail().equals(email) && objEmployee.getPassword().equals(oldPassword)) {
                            if(!oldPassword.equals(newPassword)){
                                if(newPassword.equals(confirmPassword)){
                                    EmployeeDAO.ChangePassword(oldPassword, newPassword);
                                    JOptionPane.showMessageDialog(null, "You are ChangPassword Successful");
                                    this.dispose();
                                } else{
                                    JOptionPane.showMessageDialog(null, "You are need Excaltly Password");
                                    jpwNew.setText("");
                                    jpwConfirm.setText("");
                                    jpwNew.requestFocus();
                                }
                            } else{
                                JOptionPane.showMessageDialog(null, "Don't please Password like Old Password");
                                jpwNew.setText("");
                                jpwConfirm.setText("");
                                jpwNew.requestFocus();
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "Password Error!");
                            jpwPassword.setText("");
                            jpwNew.setText("");
                            jpwConfirm.setText("");
                            jpwPassword.requestFocus();
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Email Error!");
                        txtEmail.setText("");
                        jpwPassword.setText("");
                        jpwNew.setText("");
                        jpwConfirm.setText("");
                        txtEmail.requestFocus();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Don't Connect To Server!");
                }
            } else{
                JOptionPane.showMessageDialog(null, "Please Enter Information Full!");
            }
        }
    }//GEN-LAST:event_btnOkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChangePass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChangePass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChangePass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChangePass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChangePass().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnOk;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jpwConfirm;
    private javax.swing.JPasswordField jpwNew;
    private javax.swing.JPasswordField jpwPassword;
    private javax.swing.JRadioButton rdAdmin;
    private javax.swing.JRadioButton rdEmployee;
    private javax.swing.JTextField txtEmail;
    // End of variables declaration//GEN-END:variables
}
